package cn.iocoder.yudao.module.alipay.controller.admin.infoauthaliapp;

import com.alipay.api.AlipayApiException;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.infoauthaliapp.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.infoauthaliapp.InfoAuthAliAppDO;
import cn.iocoder.yudao.module.alipay.convert.infoauthaliapp.InfoAuthAliAppConvert;
import cn.iocoder.yudao.module.alipay.service.infoauthaliapp.InfoAuthAliAppService;

@Tag(name = "管理后台 - 授权应用信息")
@RestController
@RequestMapping("/alipay/info-auth-ali-app")
@Validated
public class InfoAuthAliAppController {

    @Resource
    private InfoAuthAliAppService infoAuthAliAppService;

    @PostMapping("/create")
    @Operation(summary = "创建授权应用信息")
    @PreAuthorize("@ss.hasPermission('alipay:info-auth-ali-app:create')")
    public CommonResult<Long> createInfoAuthAliApp(@Valid @RequestBody InfoAuthAliAppCreateReqVO createReqVO) throws AlipayApiException {
        return success(infoAuthAliAppService.createInfoAuthAliApp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新授权应用信息")
    @PreAuthorize("@ss.hasPermission('alipay:info-auth-ali-app:update')")
    public CommonResult<Boolean> updateInfoAuthAliApp(@Valid @RequestBody InfoAuthAliAppUpdateReqVO updateReqVO) throws AlipayApiException {
        infoAuthAliAppService.updateInfoAuthAliApp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除授权应用信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:info-auth-ali-app:delete')")
    public CommonResult<Boolean> deleteInfoAuthAliApp(@RequestParam("id") Long id) {
        infoAuthAliAppService.deleteInfoAuthAliApp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得授权应用信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:info-auth-ali-app:query')")
    public CommonResult<InfoAuthAliAppRespVO> getInfoAuthAliApp(@RequestParam("id") Long id) {
        InfoAuthAliAppDO infoAuthAliApp = infoAuthAliAppService.getInfoAuthAliApp(id);
        return success(InfoAuthAliAppConvert.INSTANCE.convert(infoAuthAliApp));
    }

    @GetMapping("/getByAppId")
    @Operation(summary = "获得授权应用信息")
    @Parameter(name = "appId", description = "小程序id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:info-auth-ali-app:query')")
    public CommonResult<InfoAuthAliAppRespVO> getByAppId(@RequestParam("appId") String appId) {
        InfoAuthAliAppDO infoAuthAliApp = infoAuthAliAppService.getByAppId(appId);
        return success(InfoAuthAliAppConvert.INSTANCE.convert(infoAuthAliApp));
    }

    @GetMapping("/list")
    @Operation(summary = "获得授权应用信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:info-auth-ali-app:query')")
    public CommonResult<List<InfoAuthAliAppRespVO>> getInfoAuthAliAppList(@RequestParam("ids") Collection<Long> ids) {
        List<InfoAuthAliAppDO> list = infoAuthAliAppService.getInfoAuthAliAppList(ids);
        return success(InfoAuthAliAppConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得授权应用信息分页")
    @PreAuthorize("@ss.hasPermission('alipay:info-auth-ali-app:query')")
    public CommonResult<PageResult<InfoAuthAliAppRespVO>> getInfoAuthAliAppPage(@Valid InfoAuthAliAppPageReqVO pageVO) {
        PageResult<InfoAuthAliAppDO> pageResult = infoAuthAliAppService.getInfoAuthAliAppPage(pageVO);
        return success(InfoAuthAliAppConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出授权应用信息 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:info-auth-ali-app:export')")
    @OperateLog(type = EXPORT)
    public void exportInfoAuthAliAppExcel(@Valid InfoAuthAliAppExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InfoAuthAliAppDO> list = infoAuthAliAppService.getInfoAuthAliAppList(exportReqVO);
        // 导出 Excel
        List<InfoAuthAliAppExcelVO> datas = InfoAuthAliAppConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "授权应用信息.xls", "数据", InfoAuthAliAppExcelVO.class, datas);
    }

}
